import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

///自定义switch按钮
class WidgetSwitchSelect extends StatefulWidget {

  final String leftTitle;
  final String rightTitle;
  final bool isState;

  WidgetSwitchSelect({@required this.leftTitle,@required this.rightTitle, this.isState});

  @override
  _widgetSwitchSelectState createState() => _widgetSwitchSelectState();
}

class _widgetSwitchSelectState extends State<WidgetSwitchSelect> {

  bool isClick=true;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if(widget.isState!=null){
      isClick=widget.isState;
    }
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      width: ScreenUtil().setWidth(165),
      margin: EdgeInsets.only(top: 5.0),
      decoration:
          BoxDecoration(border: Border.all(color: Colors.black12, width: 0.5)),
      child: Row(
        children: <Widget>[
          _leftBtn(),
          _rightBtn(),
        ],
      ),
    );
  }

  Widget _leftBtn() {
    return Container(
      alignment: Alignment.center,
      height: ScreenUtil().setHeight(45),
      width: ScreenUtil().setWidth(81),
      decoration: BoxDecoration(
        color: isClick?Colors.blue:Colors.white,
        border: Border(left: BorderSide(color: Colors.black12, width: 0.5)),
      ),
      child: InkWell(
        onTap: () {
          setState(() {
            isClick=true;
          });
        },
        child: Text(
          widget.leftTitle,
          style: TextStyle(color:isClick?Colors.white:Colors.blue),
        ),
      ),
    );
  }

  Widget _rightBtn() {
    return InkWell(
      onTap: () {
        setState(() {
          isClick=false;
        });
      },
      child: Container(
        alignment: Alignment.center,
        height: ScreenUtil().setHeight(45),
        width: ScreenUtil().setWidth(81),
        decoration: BoxDecoration(
            color: isClick?Colors.white:Colors.blue,
            border:
                Border(right: BorderSide(color: Colors.black12, width: 0.5))),
        child: Text(
          widget.rightTitle,
          style: TextStyle(color: isClick?Colors.blue:Colors.white),
        ),
      ),
    );
  }
}
